from pyglet.gl import *
import pyglet
from pyglet.window import *
from pyglet import image
import os
"""
When you draw on a window in pyglet, you are drawing to an OpenGL context. Every window has its own context, which is created when the window is created. You can access the window's context via its context attribute.
"""

window = pyglet.window.Window(width=640, height=480, resizable=True)

xrot=yrot=zrot=0.0	# rotate the cube

def load_gl_textures():
	# load bitmaps and convert to textures
	global texture
	texture_file = os.path.join('data', 'nehe.bmp')
	texture_surf = image.load(texture_file)
	texture = texture_surf.get_texture()
	glBindTexture(GL_TEXTURE_2D, texture.id)

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

@window.event
def on_key_press(symbol, modifiers):
  if symbol == key.RETURN:
		exit()

def update(dt):
  	global xrot, yrot, zrot
	zrot+=0.5
	yrot+=0.4
	zrot+=0.3

	# Spin the objects with a constant speed

pyglet.clock.schedule(update)

@window.event
def on_resize(width, height):
	if height==0:
		height = 1
	glViewport(0, 0, width, height)

	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()

	# Calculate the aspect ratio of the window
	gluPerspective(45.0, 1.0*width/height, 0.1, 100.0)

	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	return pyglet.event.EVENT_HANDLED

	
def init():
  	"""
	Pyglet oftentimes calls this setup()	
	"""
	glEnable(GL_TEXTURE_2D)
	load_gl_textures()

  	glShadeModel(GL_SMOOTH)	# Enables smooth shading
	glClearColor(0.0, 0.0, 0.0, 0.0) #Black background
	
	glClearDepth(1.0)		# Depth buffer setup
	glEnable(GL_DEPTH_TEST)		# Enables depth testing
	glDepthFunc(GL_LEQUAL)		# The type of depth test to do

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)	# Really nice perspective calculations


@window.event
def on_draw():
  	# Here we do all the drawing
	glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT)
	glLoadIdentity()	

	glTranslatef(0.0, 0.0, -5.0) # Move left 1.5 units and into the screen 6.0
	
	glRotatef(xrot, 1.0, 0.0, 0.0)
	glRotatef(yrot, 0.0, 1.0, 0.0)
	glRotatef(zrot, 0.0, 0.0, 1.0)

	red = (1.0, 0.0, 0.0)
	green = (0.0, 1.0, 0.0)
	blue = (0.0, 0.0, 1.0)
	orange = (1.0, 0.5, 0.0)
	yellow = (1.0, 1.0, 0.0)
	violet=(1.0, 0.0, 1.0)


	glBegin(GL_QUADS)
	# Front Face
	glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0)  # bottom left
	glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, 1.0) #bottom right
	glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 1.0) #top right
	glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0) #top left
	
	# Back Face
	glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)	# Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)	# Top Right Of The Texture and Quad
	glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)	# Top Left Of The Texture and Quad
	glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)	# Bottom Left Of The Texture and Quad
	
	# Top Face
	glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)	# Top Left Of The Texture and Quad
	glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,  1.0,  1.0)	# Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,  1.0,  1.0)	# Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)	# Top Right Of The Texture and Quad
	
	# Bottom Face
	glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0)	# Top Right Of The Texture and Quad
	glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0)	# Top Left Of The Texture and Quad
	glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)	# Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)	# Bottom Right Of The Texture and Quad
	
	# Right face
	glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)	# Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)	# Top Right Of The Texture and Quad
	glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)	# Top Left Of The Texture and Quad
	glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)	# Bottom Left Of The Texture and Quad
	
	# Left Face
	glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)	# Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)	# Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0,  1.0)	# Top Right Of The Texture and Quad
	glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)	# Top Left Of The Texture and Quad
	glEnd()

	return pyglet.event.EVENT_HANDLED
  	
def m3(matrix):
  for row in matrix:
	v3(*row)
	
def v3(x,y,z):
  glVertex3f( float(x), float(y), float(z))

init()	

pyglet.app.run()
